<script setup lang="ts">
import {ref, computed, reactive, watch} from 'vue'
import {FormInstance,FormRules} from "element-plus";
import {ElMessage} from "element-plus";
import {add,edit} from "../api";

const props=defineProps({
  isShow:Boolean,
  info:Object,
  inway:Number,
  title:String
})
let visible=computed(()=>props.isShow)

const form=ref({
  Id:0,
  BookName:'',
  Author:'',
  TypeName:'',
  Remarks:''
})
const formRules=ref<FormInstance>()
const rules=reactive<FormRules>({
  BookName:[{
    required:true,message:'请输入书名',trigger:'blur'
  }],
  Author:[{
    required:true,message:'请输入作者',trigger:'blur'
  }],
  TypeName:[{
    required:true,message:'请输入类别',trigger:'blur'
  }]
})
const emit=defineEmits(["closeAdd"])
const close=()=>{
  emit('closeAdd')
}

const submit=async()=>{
  if(props.inway===0)
  {
    await add(form.value).then(()=> {
      close()
      ElMessage({
        type:'success',
        message:'添加成功'
      })
    })
  }
  else
  {
    await edit(form.value).then(()=> {
      close()
      ElMessage({
        type:'success',
        message:'修改成功'
      })
    })
  }
}
watch(()=>props.info,(newinfo)=>{
  if(newinfo){
      form.value={
        Id: newinfo.Id,
        BookName: newinfo.BookName,
        Author: newinfo.Author,
        TypeName: newinfo.TypeName,
        Remarks: newinfo.Remarks
      }
  }
})
</script>

<template>
  <el-dialog width="30%" v-model="visible" :title="title" :show-close="false">
    <el-form :model="form" label-width="60px" :rules="rules" ref="formRules">
      <el-form-item label="书名" prop="BookName">
        <el-input v-model="form.BookName"></el-input>
      </el-form-item>
      <el-form-item label="作者" prop="Author">
        <el-input v-model="form.Author"></el-input>
      </el-form-item>
      <el-form-item label="类别" prop="TypeName">
        <el-input v-model="form.TypeName"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="Remarks">
        <el-input v-model="form.Remarks"></el-input>
      </el-form-item>
    </el-form>
    <el-footer >
      <span class="form-footer">
        <el-button @click="close">cancel</el-button>
        <el-button type="primary" @click="submit">confirm</el-button>
      </span>
    </el-footer>
  </el-dialog>

</template>

<style scoped>

</style>